﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.BO.Enums;

public partial class Task_ucTaskDetail : UCBase, ITaskEditCtrl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            chkEnabled.Attributes["onclick"] = "javascript:onEnableCheckChanged(this);";
        }
        else
        {
            LoadPostBackData();
        }

        if (IsInTaskGeneration)
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "6FAC03D2-C820-438D-8EE8-56B1817A30B4",
                "    var lastFocused = null;"
                + "\r\n    function tableOnfocus(element) {"
                + "\r\n        if (lastFocused) {"
                + "\r\n            lastFocused.style.borderColor = \"#aaa\";"
                + "\r\n        }"
                + "\r\n        element.style.borderColor = \"#00aa02\";"
                + "\r\n        lastFocused = element;"
                + "\r\n    }"
                + "\r\n    function onEnableCheckChanged(e) {"
                + "\r\n        var tbl = e.parentNode.getElementsByTagName(\"table\");"
                + "\r\n        if (!e.checked) tbl.className = appendClass(tbl.className, \"disabledFont\");"
                + "\r\n        else tbl.className = removeClass(tbl.className, \"disabledFont\");"
                + "\r\n    }",
                true);
        }
    }

    void LoadPostBackData()
    {
        hfTaskIndex.Value = Request[hfTaskIndex.UniqueID];
        MsgInfo(hfTaskIndex.Value);
    }

    public void InitialControl(RFTaskInfo rfTask)
    {
        if (rfTask == null)
        {
            return;
        }

        txtTitle.Text = rfTask.TaskName;
        hfTaskIndex.Value = rfTask.TaskIndex;
        txtContent.Text = "";
        drpPriority.SelectedIndex = 0;
        drpStatus.SelectedIndex = 0;

        // TODO: add date time assigned here
        DateTime dt = DateTime.Now;
        txtStartDate.Text =dt.ToString("yyyy-MM-dd");
        txtInnerDeadline.Text = dt.AddDays(7).ToString("yyyy-MM-dd");
        txtOfficeDeadline.Text = dt.AddDays(10).ToString("yyyy-MM-dd");
        chkEnabled.Checked = true;
    }

    public bool EnableGenerateTask
    {
        get { return chkEnabled.Checked; }
        set { chkEnabled.Checked = value; }
    }

    private bool isInTaskGeneration;
    public bool IsInTaskGeneration
    {
        get { return isInTaskGeneration; }
        set { isInTaskGeneration = value; }
    }

    public void ShowTask(TaskInfo task)
    {

    }

    public bool ValidateTask()
    {
        return true;
    }

    public bool GetTask(TaskInfo task)
    {
        if (!ValidateTask())
        {
            return false;
        }

        task.Note = txtContent.Text;
        task.TaskName = txtTitle.Text;
        task.Note = txtContent.Text;
        task.Priority = (TaskPriority)int.Parse(drpPriority.SelectedValue);
        task.Status = (TaskStatus)int.Parse(drpStatus.SelectedValue);
        task.Type = TaskInfo.TaskType_Task;
        task.TaskIndex = hfTaskIndex.Value;
        if (!string.IsNullOrEmpty(txtStartDate.Text))
        {
            task.StartDateTime = DateTime.Parse(txtStartDate.Text);
        }
        if (!string.IsNullOrEmpty(txtInnerDeadline.Text))
        {
            task.InnerDeadlineDate = DateTime.Parse(txtInnerDeadline.Text);
        }
        if (!string.IsNullOrEmpty(txtOfficeDeadline.Text))
        {
            task.OuterDeadlineDate = DateTime.Parse(txtOfficeDeadline.Text);
        }
        // TODO: set other fields
        return true;
    }

    public void EditTask(TaskInfo task)
    {
        txtTitle.Text = task.TaskName;
        txtContent.Text = task.Note;
        txtStartDate.Text = task.StartDateTime.ToString("yyyy-MM-dd");
        if (task.InnerDeadlineDate != DateTime.MinValue)
        {
            txtInnerDeadline.Text = task.InnerDeadlineDate.ToString("yyyy-MM-dd");
        }
        if (task.OuterDeadlineDate != DateTime.MinValue)
        {
            txtOfficeDeadline.Text = task.OuterDeadlineDate.ToString("yyyy-MM-dd");
        }

        drpPriority.SelectedValue = ((int)task.Priority).ToString();
        drpStatus.SelectedValue = ((int)task.Status).ToString();
        hfTaskIndex.Value = task.TaskIndex;

        txtStartDate.Enabled = true;
    }
}
